package com.qf.controller;

import com.alipay.api.AlipayApiException;
import com.qf.entity.Order;
import com.qf.utils.AlipayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/AlipayController")
public class AlipayController {
    /**
     * 支付宝支付接口
     */
    @Autowired
    private AlipayUtils alipayUtils;

    //支付
    @GetMapping("/pay/{o_id}/{o_allprice}/{o_allnumber}")
    public String pay(@PathVariable("o_id") String o_id,@PathVariable("o_allprice") int o_allprice,@PathVariable("o_allnumber") int o_allnumber) {
//        System.out.println("======xxxxxxxxxxx=========="+order);
//    @GetMapping("/pay")
//    public String pay() {
//
      /*  Order payInfo = payService.getPayInfo(orderInfo);
        UserOrder userOrder = new UserOrder();
        userOrder.setUserName(payInfo.getUserName());
        userOrder.setGame(payInfo.getGame());
        userOrder.setTradeNo(payInfo.getOrderNum());
        userOrder.setOrderNum(payInfo.getNum());
        userOrder.setGame(payInfo.getGame());
        userOrder.setAmount(payInfo.getNum()*payInfo.getPrice());
        userOrderService.addOrder(userOrder);*/
        Order order = new Order();
        order.setO_id(o_id);
        order.setO_allnumber(o_allnumber);
        order.setO_allprice(o_allprice);
        System.out.println("KKKKKKKKKKKKKKK-----------------------------ddddddddddddddd------------"+order);
        String pay = "";
        try {
            pay = alipayUtils.pay(order);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return pay;
    }

    //回调
    @RequestMapping("/payNotify")
    public void payNotify(HttpServletRequest request, HttpServletResponse response) throws IOException, AlipayApiException {
        System.out.println("异步通知");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("utf-8");//乱码解决，这段代码在出现乱码时使用
        //获取支付宝POST过来反馈信息
        Map<String,String> params = new HashMap<String,String>();
        Map<String,String[]> requestParams = request.getParameterMap();
        //将异步通知中收到的所有参数都存放到map中
        for(String str :requestParams.keySet()){
            String name = str;
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            params.put(name, valueStr);
        }
        //sdk验签
//        boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.charset, AlipayConfig.sign_type); //调用SDK验证签名
//
//        if(!signVerified) {
//            System.out.println("验签失败");
//            out.print("fail");
//            return;
//        }
        System.out.println("验签成功");
        //订单号
        String out_trade_no = params.get("out_trade_no");
        //支付宝交易号
        String trade_no = params.get("trade_no");
        //付款金额
        String total_amount = params.get("total_amount");
//        //根据订单号修改用户订单状态
//        userOrderService.updateStatusByTradeNo(out_trade_no);
//        //根据订单号查询用户订单
//        UserOrder userOrder = userOrderService.selectUserOrderByTradeNo(out_trade_no);
//        //根据用户名和游戏名增加历史订单数
//        goodsService.addHistoryOrderByNameAndGame(userOrder);
//        //下单成功将订单信息存储到接单玩家的订单
//        goodsService.addGoodsOrder(userOrder);

    }

}

